home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 276_01 / a684util.c < prev    next >
C/C++ Source or Header  |  1989-10-01  |  13KB  |  478 lines

  1. /*
  2.     HEADER:        CUG276;
  3.     TITLE:        6804 Cross-Assembler (Portable);
  4.     FILENAME:    A684UTIL.C;
  5.     VERSION:    0.1;
  6.     DATE:        08/27/1988;
  7.  
  8.     DESCRIPTION:    "This program lets you use your computer to assemble
  9.             code for the Motorola 6804 family microprocessors.
  10.             The program is written in portable C rather than BDS
  11.             C.  All    assembler features are supported except
  12.             relocation linkage, and macros.";
  13.  
  14.     KEYWORDS:    Software Development, Assemblers, Cross-Assemblers,
  15.             Motorola, MC6804;
  16.  
  17.     SYSTEM:        CP/M-80, CP/M-86, HP-UX, MSDOS, PCDOS, QNIX;
  18.     COMPILERS:    Aztec C86, Aztec CII, CI-C86, Eco-C, Eco-C88, HP-UX,
  19.             Lattice C, Microsoft C,    QNIX C;
  20.  
  21.     WARNINGS:    "This program should compile on any full-featured C
  22.             compiler.  Subset compilers such as Toolworks C and
  23.             BDS C will present substantial difficulties."
  24.  
  25.     AUTHORS:    William C. Colley III;
  26. */
  27.  
  28. /*
  29.               6804 Cross-Assembler in Portable C
  30.  
  31.         Copyright (c) 1985, 1988 William C. Colley, III
  32.  
  33. Revision History:
  34.  
  35. Ver    Date        Description
  36.  
  37. 0.0    MAR 1988    Adapted from version 0.2 of the portable 6805 cross-
  38.             assembler which was adapted from version 3.2 of the
  39.             portable 6801 cross-assembler.  WCC3.
  40.  
  41. 0.1    AUG 1988    Fixed a bug in the command line parser that puts it
  42.             into a VERY long loop if the user types a command line
  43.             like "A684 FILE.ASM -L".  WCC3 per Alex Cameron.
  44.  
  45. This module contains the following utility packages:
  46.  
  47.     1)  symbol table building and searching
  48.  
  49.     2)  opcode and operator table searching
  50.  
  51.     3)  listing file output
  52.  
  53.     4)  hex file output
  54.  
  55.     5)  error flagging
  56. */
  57.  
  58. /*  Get global goodies:  */
  59.  
  60. #include "a684.h"
  61.  
  62. /*  Make sure that MSDOS compilers using the large memory model know    */
  63. /*  that calloc() returns pointer to char as an MSDOS far pointer is    */
  64. /*  NOT compatible with the int type as is usually the case.        */
  65.  
  66. char *calloc();
  67.  
  68. /*  Get access to global mailboxes defined in A684.C:            */
  69.  
  70. extern char errcode, line[], title[];
  71. extern int eject, listhex;
  72. extern unsigned address, bytes, errors, listleft, obj[], pagelen;
  73.  
  74. /*  The symbol table is a binary tree of variable-length blocks drawn    */
  75. /*  from the heap with the calloc() function.  The root pointer lives    */
  76. /*  here:                                */
  77.  
  78. static SYMBOL *sroot = NULL;
  79.  
  80. /*  Add new symbol to symbol table.  Returns pointer to symbol even if    */
  81. /*  the symbol already exists.  If there's not enough memory to store    */
  82. /*  the new symbol, a fatal error occurs.                */
  83.  
  84. SYMBOL *new_symbol(nam)
  85. char *nam;
  86. {
  87.     SCRATCH int i;
  88.     SCRATCH SYMBOL **p, *q;
  89.     void fatal_error();
  90.  
  91.     for (p = &sroot; (q = *p) && (i = strcmp(nam,q -> sname)); )
  92.     p = i < 0 ? &(q -> left) : &(q -> right);
  93.     if (!q) {
  94.     if (!(*p = q = (SYMBOL *)calloc(1,sizeof(SYMBOL) + strlen(nam))))
  95.         fatal_error(SYMBOLS);
  96.     strcpy(q -> sname,nam);
  97.     }
  98.     return q;
  99. }
  100.  
  101. /*  Look up symbol in symbol table.  Returns pointer to symbol or NULL    */
  102. /*  if symbol not found.                        */
  103.  
  104. SYMBOL *find_symbol(nam)
  105. char *nam;
  106. {
  107.     SCRATCH int i;
  108.     SCRATCH SYMBOL *p;
  109.  
  110.     for (p = sroot; p && (i = strcmp(nam,p -> sname));
  111.     p = i < 0 ? p -> left : p -> right);
  112.     return p;
  113. }
  114.  
  115. /*  Opcode table search routine.  This routine pats down the opcode    */
  116. /*  table for a given opcode and returns either a pointer to it or    */
  117. /*  NULL if the opcode doesn't exist.                    */
  118.  
  119. OPCODE *find_code(nam)
  120. char *nam;
  121. {
  122.     OPCODE *bsearch();
  123.  
  124.     static OPCODE opctbl[] = {
  125.     { IMMEDOK + ARITHOP,                0xe2,    "ADD"    },
  126.     { IMMEDOK + ARITHOP,                0xe5,    "AND"    },
  127.     { AREG + IMPLIED,                0xfa,    "ASLA"    },
  128.     { FLAGJMP,                    0x40,    "BCC"    },
  129.     { BITOP,                    0xd0,    "BCLR"    },
  130.     { FLAGJMP,                    0x60,    "BCS"    },
  131.     { FLAGJMP,                    0x20,    "BEQ"    },
  132.     { FLAGJMP,                    0x40,    "BHS"    },
  133.     { FLAGJMP,                    0x60,    "BLO"    },
  134.     { FLAGJMP,                    0x00,    "BNE"    },
  135.     { BITJMP,                    0xc0,    "BRCLR"    },
  136.     { BITJMP,                    0xc8,    "BRSET"    },
  137.     { BITOP,                    0xd8,    "BSET"    },
  138.     { AREG + IMPLIED,                0xfb,    "CLRA"    },
  139.     { ZERO + XREG + IMPLIED,            0xb0,    "CLRX"    },
  140.     { ZERO + YREG + IMPLIED,            0xb0,    "CLRY"    },
  141.     { IMMEDOK + ARITHOP,                0xe4,    "CMP"    },
  142.     { IMPLIED,                    0xb4,    "COMA"    },
  143.     { SHORTOK + ARITHOP,                0xe7,    "DEC"    },
  144.     { AREG + IMPLIED,                0xff,    "DECA"    },
  145.     { IMPLIED,                    0xe7,    "DECX"    },
  146.     { IMPLIED,                    0xf7,    "DECY"    },
  147.     { PSEUDO + ISIF,                ELSE,    "ELSE"    },
  148.     { PSEUDO,                    END,    "END"    },
  149.     { PSEUDO + ISIF,                ENDIF,    "ENDIF"    },
  150.     { PSEUDO,                    EQU,    "EQU"    },
  151.     { PSEUDO,                    FCB,    "FCB"    },
  152.     { PSEUDO,                    FCC,    "FCC"    },
  153.     { PSEUDO,                    FDB,    "FDB"    },
  154.     { PSEUDO + ISIF,                IF,    "IF"    },
  155.     { SHORTOK + ARITHOP,                0xe6,    "INC"    },
  156.     { AREG + IMPLIED,                0xfe,    "INCA"    },
  157.     { PSEUDO,                    INCL,    "INCL"    },
  158.     { IMPLIED,                    0xe6,    "INCX"    },
  159.     { IMPLIED,                    0xf6,    "INCY"    },
  160.     { LONGJMP,                    0x90,    "JMP"    },
  161.     { LONGJMP,                    0x80,    "JSR"    },
  162.     { SHORTOK + IMMEDOK + ARITHOP,            0xe0,    "LDA"    },
  163.     { XREG + IMMED,                    0xb0,    "LDXI"    },
  164.     { YREG + IMMED,                    0xb0,    "LDYI"    },
  165.     { DIR_IMMED,                    0xb0,    "MVI"    },
  166.     { IMPLIED,                    0x20,    "NOP"    },
  167.     { PSEUDO,                    ORG,    "ORG"    },
  168.     { PSEUDO,                    PAGE,    "PAGE"    },
  169.     { PSEUDO,                    RMB,    "RMB"    },
  170.     { IMPLIED,                    0xb5,    "ROLA"    },
  171.     { IMPLIED,                    0xb2,    "RTI"    },
  172.     { IMPLIED,                    0xb3,    "RTS"    },
  173.     { PSEUDO,                    SET,    "SET"    },
  174.     { SHORTOK + ARITHOP,                0xe1,    "STA"    },
  175.     { IMPLIED,                    0xb6,    "STOP"    },
  176.     { IMMEDOK + ARITHOP,                0xe3,    "SUB"    },
  177.     { IMPLIED,                    0xbc,    "TAX"    },
  178.     { IMPLIED,                    0xbd,    "TAY"    },
  179.     { PSEUDO,                    TITLE,    "TITLE"    },
  180.     { IMPLIED,                    0xac,    "TXA"    },
  181.     { IMPLIED,                    0xad,    "TYA"    },
  182.     { IMPLIED,                    0xb7,    "WAIT"    }
  183.     };
  184.  
  185.     return bsearch(opctbl,opctbl + (sizeof(opctbl) / sizeof(OPCODE)),nam);
  186. }
  187.  
  188. /*  Operator table search routine.  This routine pats down the        */
  189. /*  operator table for a given operator and returns either a pointer    */
  190. /*  to it or NULL if the opcode doesn't exist.                */
  191.  
  192. OPCODE *find_operator(nam)
  193. char *nam;
  194. {
  195.     OPCODE *bsearch();
  196.  
  197.     static OPCODE oprtbl[] = {
  198.     { BINARY + LOG1  + OPR,        AND,        "AND"    },
  199.     { BINARY + RELAT + OPR,        '=',        "EQ"    },
  200.     { BINARY + RELAT + OPR,        GE,        "GE"    },
  201.     { BINARY + RELAT + OPR,        '>',        "GT"    },
  202.     { UNARY  + UOP3  + OPR,        HIGH,        "HIGH"    },
  203.     { BINARY + RELAT + OPR,        LE,        "LE"    },
  204.     { UNARY  + UOP3  + OPR,        LOW,        "LOW"    },
  205.     { BINARY + RELAT + OPR,        '<',        "LT"    },
  206.     { BINARY + MULT  + OPR,        MOD,        "MOD"    },
  207.     { BINARY + RELAT + OPR,        NE,        "NE"    },
  208.     { UNARY  + UOP2  + OPR,        NOT,        "NOT"    },
  209.     { BINARY + LOG2  + OPR,        OR,        "OR"    },
  210.     { BINARY + MULT  + OPR,        SHL,        "SHL"    },
  211.     { BINARY + MULT  + OPR,        SHR,        "SHR"    },
  212.     { REG,                'X',        "X"    },
  213.     { BINARY + LOG2  + OPR,        XOR,        "XOR"    },
  214.     { REG,                'Y',        "Y"    }
  215.     };
  216.  
  217.     return bsearch(oprtbl,oprtbl + (sizeof(oprtbl) / sizeof(OPCODE)),nam);
  218. }
  219.  
  220. static OPCODE *bsearch(lo,hi,nam)
  221. OPCODE *lo, *hi;
  222. char *nam;
  223. {
  224.     SCRATCH int i;
  225.     SCRATCH OPCODE *chk;
  226.  
  227.     for (;;) {
  228.     chk = lo + (hi - lo) / 2;
  229.     if (!(i = ustrcmp(chk -> oname,nam))) return chk;
  230.     if (chk == lo) return NULL;
  231.     if (i < 0) lo = chk;
  232.     else hi = chk;
  233.     }
  234. }
  235.  
  236. static int ustrcmp(s,t)
  237. char *s, *t;
  238. {
  239.     SCRATCH int i;
  240.  
  241.     while (!(i = toupper(*s++) - toupper(*t)) && *t++);
  242.     return i;
  243. }
  244.  
  245. /*  Buffer storage for line listing routine.  This allows the listing    */
  246. /*  output routines to do all operations without the main routine    */
  247. /*  having to fool with it.                        */
  248.  
  249. static FILE *list = NULL;
  250.  
  251. /*  Listing file open routine.  If a listing file is already open, a    */
  252. /*  warning occurs.  If the listing file doesn't open correctly, a    */
  253. /*  fatal error occurs.  If no listing file is open, all calls to    */
  254. /*  lputs() and lclose() have no effect.                */
  255.  
  256. void lopen(nam)
  257. char *nam;
  258. {
  259.     FILE *fopen();
  260.     void fatal_error(), warning();
  261.  
  262.     if (list) warning(TWOLST);
  263.     else if (!(list = fopen(nam,"w"))) fatal_error(LSTOPEN);
  264.     return;
  265. }
  266.  
  267. /*  Listing file line output routine.  This routine processes the    */
  268. /*  source line saved by popc() and the output of the line assembler in    */
  269. /*  buffer obj into a line of the listing.  If the disk fills up, a    */
  270. /*  fatal error occurs.                            */
  271.  
  272. void lputs()
  273. {
  274.     SCRATCH int i, j;
  275.     SCRATCH unsigned *o;
  276.     void check_page(), fatal_error();
  277.  
  278.     if (list) {
  279.     i = bytes;  o = obj;
  280.     do {
  281.         fprintf(list,"%c  ",errcode);
  282.         if (listh